<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/11
 * Time: 13:37
 */

class SapApi extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('api_model');
        $this->load->model('api_model/sap_model','sap_model');
    }

    /**
     * 获取维修有配件订单
     */
    public function GetMaintainOrder(){
        $data=$this->api_model->GetMaintainOrder();
        foreach($data as $key=>$val){
            $parts=$this->api_model->GetMaterial($val['order_num']);
            if($parts) {
                $data[$key]['parts']=$parts;
                $code=$this->api_model->GetWarehouse($val['service_center'],$val['brandCustomerNum']);
                if($code==''){
                    $code=$this->GetMaintenanceWarehouseCode($val['point']);
                }
                $data[$key]['warehouse_code']=$code;
            }else{
                unset($data[$key]);
            }
        }
        if($data){
            echo json_encode($data);
        }else{
            echo json_encode(array('code'=>2,'msg'=>'data null'));
        }
    }

    /**
     * 更新订单同步状态
     */
    public function UpdateMaintainOrderSyncSapStatus(){
        $gp=gp();
        if($gp['id']) {
            $update = $this->api_model->UpdateMaintainOrderSyncSapStatus($gp['id'],$gp['val']);
            if ($update) {
                echo json_encode(array('code' => 1, 'msg' => 'ok'));
            } else {
                echo json_encode(array('code' => 2, 'msg' => 'Update failed'));
            }
        }else{
            echo json_encode(array('code' => 3, 'msg' => 'id is null'));
        }
    }

    /**
     * 获取SAP产品分类数据
     */
    public function GetSapData(){
        //获取并插入产品类型数据
        $typeData=$this->GetClassData('Qkey_BigClass','product_type');
        if($typeData){
            //获取并插入产品品牌数据
            $brandData=$this->GetClassData('Qkey_MiddleClass','product_brand');
            if($brandData){
                //获取并插入产品类型和品牌关联数据
                $associationDataA=$this->GetAssociationDataA('Qkey_BigMiddel','product_association_a');
                if($associationDataA) {
                    //获取并插入产品型号数据
                    $modelData = $this->GetClassData('Qkey_SmallClass', 'product_model');
                    if($modelData){
                        //获取并插入产品类型和品牌以及模型关联数据
                        $associationDataB=$this->GetAssociationDataB('Qkey_MiddelSmall','product_association_b');
                        if($associationDataB){
                            echo 'ok';
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取产品类型数据
     * @param $type
     * @param $table
     */
    public function GetClassData($type,$table){
        //获取产品类型数据
        $url='http://47.90.7.103:60002/WebUnit/GetData?UserSign=1&QKey='.$type;
        $data=$this->CallAPIQuick($url);
        $addResult=false;
        if($data){
            //清空产品类型数据
            $result=$this->sap_model->TruncateTable($table);
            //插入产品类型数据
            if($result){
                foreach($data as $row){
                    $addData=array(
                        'sap_id'=>$row->U_ID,
                        'number'=>$row->U_Code,
                        'name'=>$row->U_Name,
                        'name_en'=>$row->U_FName
                    );
                    $addResult=$this->sap_model->AddProductClassData($table,$addData);
                    if($addResult==false){
                        exit();
                    }
                }
            }
        }
        return $addResult;
    }

    /**
     * 获取关联数据A(类型对品牌)
     * @param $type
     * @param $table
     */
    public function GetAssociationDataA($type,$table){
        //获取产品类型数据
        $url='http://47.90.7.103:60002/WebUnit/GetData?UserSign=1&QKey='.$type;
        $data=$this->CallAPIQuick($url);
        $addResult=false;
        if($data){
            //清空产品类型数据
            $result=$this->sap_model->TruncateTable($table);
            //插入产品类型数据
            if($result){
                foreach($data as $row){
                    $addData=array(
                        'sap_id'=>$row->U_ID,
                        'bid'=>$row->U_MID,
                        'tid'=>$row->U_BID
                    );
                    $addResult=$this->sap_model->AddProductClassData($table,$addData);
                    if($addResult==false){
                        exit();
                    }
                }
            }
        }
        return $addResult;
    }
    /**
     * 获取关联数据B(类型-品牌对模型)
     * @param $type
     * @param $table
     */
    public function GetAssociationDataB($type,$table){
        //获取产品类型数据
        $url='http://47.90.7.103:60002/WebUnit/GetData?UserSign=1&QKey='.$type;
        $data=$this->CallAPIQuick($url);
        $addResult=false;
        if($data){
            //清空产品类型数据
            $result=$this->sap_model->TruncateTable($table);
            //插入产品类型数据
            if($result){
                foreach($data as $row){
                    $addData=array(
                        'sap_id'=>$row->U_ID,
                        'bid'=>$row->U_MID,
                        'tid'=>$row->U_BID,
                        'mid'=>$row->U_SID
                    );
                    $addResult=$this->sap_model->AddProductClassData($table,$addData);
                    if($addResult==false){
                        exit();
                    }
                }
            }
        }
        return $addResult;
    }
    /**
     * SAP CURL
     * @param $url
     * @return mixed
     */
    public function CallAPIQuick($url)
    {
        $username='hcc';
        $password='1234';
        $authentication = base64_encode($username.":".$password);
        $header = array ();
        $header[]='Authorization:Basic '.$authentication;
        $header[]='Content-Type:application/json;charset=utf-8';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        //curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');
        //curl_setopt($ch,CURLOPT_POSTFIELDS,$params);//POST数据
        $response=curl_exec($ch);
        //echo $response;
        if(curl_errno($ch))
        {//出错则显示错误信息
            echo curl_error($ch);
        }
        curl_close($ch); //关闭curl链接
        $jsonObject=json_decode($response);
        return $jsonObject;
    }
    /**
     * 获取维保仓代码
     * @param string $site
     * @return string
     */
    public function GetMaintenanceWarehouseCode($site=''){
        $stockStr='';
        switch(strtolower($site)){
            case 'es':
                $stockStr = 'W005-T03';
                break;
            case 'de':
                $stockStr = 'W014-T01';
                break;
            case 'ru':
                $stockStr = 'W006-T02';
                break;
            case 'us':
                $stockStr = 'W032-T06';
                break;
            case 'uk':
                $stockStr = 'W033-T05';
                break;
        }
        return $stockStr;
    }
}